package net.lookyou.boot.demo.utils;

import lombok.extern.slf4j.Slf4j;
import net.lookyou.boot.demo.common.Constants;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.codec.Hex;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.security.MessageDigest;

/**
 *  <p> 加密工具 </p>
 *
 * @description:
 * @author: zhengqing
 * @date: 2019/10/13 0013 15:25
 */
@Slf4j
public class PasswordUtils {

    /**
     * 校验密码是否一致
     *
     * @param password: 前端传过来的密码
     * @param hashedPassword：数据库中储存加密过后的密码
     * @param salt：盐值
     * @return
     */
    public static boolean isValidPassword(String password, String hashedPassword, String salt, PasswordEncoder passwordEncoder) {
        String encoderPass = password.concat(salt);
        boolean check = passwordEncoder.matches(encoderPass,hashedPassword);
        return check;
    }



    public static void main(String[] args) {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encoderPass = "123456".concat("salt");
        System.out.println(passwordEncoder.encode(encoderPass));
        /*String pass = "5a7874c1ce05340069f9adaff4a6c8d3ab2bb2a0";
        System.out.println(passwordEncoder.matches(encoderPass, pass));
        System.out.println(passwordEncoder.encode(encoderPass));*/
    }
}
